<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AjaxGrid
 *
 * @author juwking
 */
class AjaxGrid extends ComponentBase {

    protected static $objInstance = null;
    public $pager, $criteria;
    public $page;

    public function __construct() {
        parent::__construct();
        $this->criteria = new Criteria();
        $this->pager = new PropelPager($this->criteria, 'ClientPeer', 'doSelect', 1, $rowsPerPage = 30);
        $this->id = rand();
    }

    public function display() {
        $this->assign('pageindexes', range(0, $this->pager->getTotalPages()));
        $this->assign('messageContent', 'test');
        $this->smarty->display('grid/form.tpl');
    }

    public function setCriteria($critarray) {
        $c = new Criteria();

        $criterias = array();
        if (!empty($critarray['name'])) {
            $crit0 = $c->getNewCriterion(ClientPeer::NAME, '%' . $critarray['name'] . '%', Criteria::LIKE);
            array_push(&$criterias, $crit0);
        }
        if (!empty($critarray['surname'])) {
            $crit0 = $c->getNewCriterion(ClientPeer::SURNAME, '%' . $critarray['surname'] . '%', Criteria::LIKE);
            array_push(&$criterias, $crit0);
        }
        if (!empty($critarray['agedown'])) {
            $crit0 = $c->getNewCriterion(ClientPeer::AGE, (int) $critarray['agedown'], Criteria::GREATER_THAN);
            array_push(&$criterias, $crit0);
        }
        if (!empty($critarray['agedown'])) {
            $crit0 = $c->getNewCriterion(ClientPeer::AGE, (int) $critarray['ageup'], Criteria::LESS_THAN);
            array_push(&$criterias, $crit0);
        }
        foreach ($criterias as $key => $value) {
            $c->add($value);
        }


        $this->criteria = $c;
    }

    public function displayPage($page) {
        $this->pager = new PropelPager($this->criteria, 'ClientPeer', 'doSelect', $_REQUEST['page'], $rowsPerPage = 30);
        $this->assign('pages', $this->pager);
        $this->smarty->display('grid/client.tpl');
    }

    public function addColumn($column) {
        #$this->bonusColumns
    }

    public static function getInstance() {
        if (is_null(self::$objInstance)) {
            self::$objInstance = new self();
        }

        return self::$objInstance;
    }

}

?>
